﻿//using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Globalization;
//using System.Linq;
//using System.Text;
//using System.Threading;
//using System.Web;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using Telerik.Web.UI;
//using System.Web.UI.HtmlControls;

//namespace TelerikHelper
//{
//    [DefaultProperty("SelectedDate")]
//    [ToolboxData("<{0}:AdvancedDateTimePicker runat=server></{0}:AdvancedDateTimePicker>")]
//    public class AdvancedDateTimePicker_old : CompositeControl
//    {
//        public Dictionary<string, string> Cultures
//        {
//            get
//            {
//                if (this.ViewState["Cultures"] == null)
//                {
//                    this.ViewState["Cultures"] = new Dictionary<string, string>() { { "هـ", "ar-sa" }, { "م", "en" } };
//                }
//                return (Dictionary<string, string>)this.ViewState["Cultures"];
//            }
//            set
//            {
//                this.ViewState["Cultures"] = value;
//            }
//        }
//        public DateTime? MinDate
//        {
//            get
//            {
//                return (DateTime?)this.ViewState["MinDate"];
//            }
//            set
//            {
//                this.ViewState["MinDate"] = value;
//            }
//        }
//        public DateTime? MaxDate
//        {
//            get
//            {
//                return (DateTime?)this.ViewState["MaxDate"];
//            }
//            set
//            {
//                this.ViewState["MaxDate"] = value;
//            }
//        }

//        [Bindable(true)]
//        [Category("Appearance")]
//        [DefaultValue(null)]
//        [Localizable(true)]
//        public DateTime? SelectedDate
//        {
//            get
//            {
//                this.EnsureChildControls(); 
//                return this.getDate();
//            }
//            set
//            {
//                this.EnsureChildControls();
//                this.setDateControls(value);
//                this.setTimeControl(value);
//            }
//        }

//        public object DBSelectedDate
//        {
//            get
//            {
//                return this.SelectedDate.HasValue ? (object)this.SelectedDate.Value : DBNull.Value;            
//            }
//            set
//            {
//                this.SelectedDate = (value == DBNull.Value ? (DateTime?)null : (DateTime?)value);
//            }

//        }


//        public RadNumericTextBox RadNumericTextBoxYear=null;
//        public RadComboBox RadComboBoxMonth = null;
//        public RadButton RadButtonDate = null;
//        public RadDock RadDock = null;
//        public RadTimePicker RadTimePickerTime = null;
//        public RadComboBox RadComboBoxCaluture = null;
//        public RadButton RadButtonClear = null;
//        public System.Web.UI.WebControls.Calendar Calendar = null;
//        public RadButton RadButtonToday = null;

//        public AdvancedDateTimePicker_old(): base()
//        {
//            this.Load+=AdvancedDateTimePicker_Load;
//        }

//        protected override void OnInit(EventArgs e)
//        {
//            base.OnInit(e);
//            RadScriptManager.RegisterClientScriptResource(this,this.GetType(), "TelerikHelper.Scripts.HelperMethods.js");
//        }
//        protected override void CreateChildControls()
//        {
//            if (!ChildControlsCreated)
//            {
//                this.Controls.Clear();
                               
//                //generic Div
//                HtmlGenericControl DivContainer = new HtmlGenericControl("div");
//                DivContainer.ID = "DivContainer";
//                this.Controls.Add(DivContainer);

//                //ul
//                HtmlGenericControl UL = new HtmlGenericControl("ul");
//                UL.Style["margin"] = "0px";
//                UL.Style["padding"] = "0px";
//                UL.Style["list-style-type"] = "none";
//                DivContainer.Controls.Add(UL);

//                //li
//                HtmlGenericControl LI = new HtmlGenericControl("li");
//                LI.Style["margin"] = "0px";
//                LI.Style["padding"] = "1px";
//                LI.Style["float"] = "left";
//                UL.Controls.Add(LI);

//                //RadButtonDate
//                this.RadButtonDate = new RadButton();
//                this.RadButtonDate.ID = "RadButtonDate";
//                this.RadButtonDate.AutoPostBack = false;
//                this.RadButtonDate.Text = "...";
//                this.RadButtonDate.Width = Unit.Pixel(35);
//                this.RadButtonDate.Style["margin"] = "2px";
//                this.RadButtonDate.CausesValidation = false;
//                LI.Controls.Add(this.RadButtonDate);

                

//                //li
//                LI = new HtmlGenericControl("li");
//                LI.Style["margin"] = "0px";
//                LI.Style["padding"] = "1px";
//                LI.Style["float"] = "left";
//                UL.Controls.Add(LI);

//                //RadComboBoxMonth
//                this.RadComboBoxMonth = new RadComboBox();
//                this.RadComboBoxMonth.ID = "RadComboBoxMonth";
//                this.RadComboBoxMonth.SelectedIndexChanged += RadComboBoxMonth_SelectedIndexChanged;
//                this.RadComboBoxMonth.AutoPostBack = true;
//                this.RadComboBoxMonth.Width = Unit.Pixel(110);
//                this.RadComboBoxMonth.Style["margin"] = "2px";
//                this.RadComboBoxMonth.AllowCustomText = false;
//                this.RadComboBoxMonth.Filter = RadComboBoxFilter.Contains;
//                this.RadComboBoxMonth.EnableTextSelection = true;
//                this.RadComboBoxMonth.MarkFirstMatch = true;
//                this.RadComboBoxMonth.ShowDropDownOnTextboxClick = true;
//                this.RadComboBoxMonth.CausesValidation = false;
//                LI.Controls.Add(this.RadComboBoxMonth);

//                //li
//                LI = new HtmlGenericControl("li");
//                LI.Style["margin"] = "0px";
//                LI.Style["padding"] = "1px";
//                LI.Style["float"] = "left";
//                UL.Controls.Add(LI);

//                //RadNumericTextBoxYear
//                this.RadNumericTextBoxYear = new RadNumericTextBox();
//                this.RadNumericTextBoxYear.ID = "RadNumericTextBoxYear";
//                this.RadNumericTextBoxYear.TextChanged += RadNumericTextBoxYear_TextChanged;
//                this.RadNumericTextBoxYear.Width = Unit.Pixel(50);
//                this.RadNumericTextBoxYear.NumberFormat.DecimalDigits = 0;
//                this.RadNumericTextBoxYear.NumberFormat.GroupSeparator = "";
//                this.RadNumericTextBoxYear.Style["margin"] = "2px";
//                this.RadNumericTextBoxYear.CausesValidation = false;
//                LI.Controls.Add(this.RadNumericTextBoxYear);

//                //li
//                LI = new HtmlGenericControl("li");
//                LI.Style["clear"] = "both";
//                UL.Controls.Add(LI);


//                //RadDockZone
//                RadDockZone RadDockZone = new RadDockZone();
//                RadDockZone.MinHeight = Unit.Pixel(0);
//                RadDockZone.BorderColor = System.Drawing.Color.Transparent;
//                RadDockZone.FitDocks = true;
//                RadDockZone.Width = Unit.Pixel(300);
//                RadDockZone.Style["position"] = "absolute";
//                RadDockZone.Style["z-index"] = "100000000";
//                DivContainer.Controls.Add(RadDockZone);

//                //RadDock
//                this.RadDock = new RadDock();
//                this.RadDock.ID = "RadDock";
//                this.RadDock.UniqueName = "RadDock_" + this.ClientID;
//                this.RadDock.Closed = true;
//                this.RadDock.DefaultCommands = Telerik.Web.UI.Dock.DefaultCommands.None;
//                this.RadDock.DockHandle = DockHandle.None;
//                this.RadDock.EnableRoundedCorners = false;
//                this.RadDock.Resizable = true;
//                RadDockZone.Controls.Add(this.RadDock);
//                this.RadButtonDate.OnClientClicked = "function(){ reverseCollapse('" + this.RadDock.ClientID + "');}";


//                //ul
//                UL = new HtmlGenericControl("ul");
//                UL.Style["margin"] = "0px";
//                UL.Style["padding"] = "0px";
//                UL.Style["list-style-type"] = "none";
//                RadDock.ContentContainer.Controls.Add(UL);


//                //li
//                LI = new HtmlGenericControl("li");
//                LI.Style["margin"] = "0px";
//                LI.Style["padding"] = "1px";
//                LI.Style["float"] = "left";
//                UL.Controls.Add(LI);


//                //RadTimePickerTime
//                this.RadTimePickerTime = new RadTimePicker();
//                this.RadTimePickerTime.ID = "RadTimePickerTime";
//                this.RadTimePickerTime.Width = Unit.Pixel(93);
//                this.RadTimePickerTime.Style["margin"] = "2px";
//                LI.Controls.Add(this.RadTimePickerTime);


//                //li
//                LI = new HtmlGenericControl("li");
//                LI.Style["margin"] = "0px";
//                LI.Style["padding"] = "1px";
//                LI.Style["float"] = "left";
//                UL.Controls.Add(LI);


//                //RadComboBoxCaluture
//                this.RadComboBoxCaluture = new RadComboBox();
//                this.RadComboBoxCaluture.ID = "RadComboBoxCaluture";
//                this.RadComboBoxCaluture.SelectedIndexChanged += RadComboBoxCaluture_SelectedIndexChanged;
//                this.RadComboBoxCaluture.AutoPostBack = true;
//                this.RadComboBoxCaluture.Width = Unit.Pixel(75);
//                this.RadComboBoxCaluture.Style["margin"] = "2px";
//                this.RadComboBoxCaluture.AllowCustomText = false;
//                this.RadComboBoxCaluture.Filter = RadComboBoxFilter.Contains;
//                this.RadComboBoxCaluture.MarkFirstMatch = true;
//                this.RadComboBoxCaluture.EnableTextSelection = true;
//                this.RadComboBoxCaluture.ShowDropDownOnTextboxClick = true;
//                this.RadComboBoxCaluture.CausesValidation = false;
//                LI.Controls.Add(this.RadComboBoxCaluture);

//                //li
//                LI = new HtmlGenericControl("li");
//                LI.Style["margin"] = "0px";
//                LI.Style["padding"] = "1px";
//                LI.Style["float"] = "left";
//                UL.Controls.Add(LI);



//                //RadButtonClear
//                this.RadButtonClear = new RadButton();
//                this.RadButtonClear.ID = "RadButtonClear";
//                this.RadButtonClear.Click += RadButtonClear_Click;
//                this.RadButtonClear.Icon.SecondaryIconCssClass = "rbRemove";
//                this.RadButtonClear.Text = "";
//                this.RadButtonClear.Style["margin"] = "2px";
//                this.RadButtonClear.CausesValidation = false;
//                LI.Controls.Add(this.RadButtonClear);

//                //li
//                LI = new HtmlGenericControl("li");
//                LI.Style["clear"] = "both";
//                UL.Controls.Add(LI);

//                //Calendar
//                this.Calendar = new System.Web.UI.WebControls.Calendar();
//                this.Calendar.ID = "Calendar";
//                this.Calendar.SelectionChanged += Calendar_SelectionChanged;
//                this.Calendar.DayRender += Calendar_DayRender;
//                this.Calendar.ShowNextPrevMonth = false;
//                this.Calendar.ShowTitle = false;
//                this.Calendar.TodayDayStyle.Font.Size = FontUnit.XLarge;
//                this.Calendar.DayStyle.Font.Bold = true;
//                this.Calendar.OtherMonthDayStyle.Font.Bold = false;
//                this.Calendar.OtherMonthDayStyle.Font.Size = FontUnit.XSmall;
//                this.Calendar.Style["margin"] = "2px";
//                RadDock.ContentContainer.Controls.Add(this.Calendar);

//                //generic Div
//                HtmlGenericControl div = new HtmlGenericControl("div");
//                div.Style["width"] = "80px";
//                div.Style["margin"] = "0 auto";
//                RadDock.ContentContainer.Controls.Add(div);

//                //RadButtonToday
//                this.RadButtonToday = new RadButton();
//                this.RadButtonToday.ID = "RadButtonToday";
//                this.RadButtonToday.Click += RadButtonToday_Click;
//                this.RadButtonToday.CausesValidation = false;
//                div.Controls.Add(this.RadButtonToday);

//                this.ChildControlsCreated = true;

//            }
//        }
//        protected override void Render(System.Web.UI.HtmlTextWriter writer)
//        {
//            if (this.RadComboBoxCaluture.SelectedValue != null)
//            {
//                CultureInfo CurrentCulture = Thread.CurrentThread.CurrentCulture;
//                CultureInfo CurrentUICulture = Thread.CurrentThread.CurrentUICulture;
//                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(this.RadComboBoxCaluture.SelectedValue);
//                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(this.RadComboBoxCaluture.SelectedValue);
//                base.Render(writer);
//                Thread.CurrentThread.CurrentCulture = CurrentCulture;
//                Thread.CurrentThread.CurrentUICulture = CurrentUICulture;
//            }
//        }
//        protected override HtmlTextWriterTag TagKey
//        {
//            get
//            {
//                return HtmlTextWriterTag.Div;
//            }
//        }

//        protected void AdvancedDateTimePicker_Load(object sender, EventArgs e)
//        {
//            this.EnsureChildControls();
//            if (!this.Page.IsPostBack)
//            {
//                this.bindCulters();
//                this.setYearAllowedRange();
//                this.getMonthes();
//                this.Calendar.VisibleDate = DateTime.Now.Date;
//                this.RadButtonToday.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
//            }
//            else
//            {
//                //((HtmlGenericControl)this.RadButtonDate.Parent).Style["float"] = this.HiddenFieldFloat.Value;
//                //((HtmlGenericControl)this.RadComboBoxMonth.Parent).Style["float"] = this.HiddenFieldFloat.Value;
//                //((HtmlGenericControl)this.RadNumericTextBoxYear.Parent).Style["float"] = this.HiddenFieldFloat.Value;
//                //((HtmlGenericControl)this.RadTimePickerTime.Parent).Style["float"] = this.HiddenFieldFloat.Value;
//                //((HtmlGenericControl)this.RadComboBoxCaluture.Parent).Style["float"] = this.HiddenFieldFloat.Value;
//                //((HtmlGenericControl)this.RadButtonClear.Parent).Style["float"] = this.HiddenFieldFloat.Value;
//            }
            
//            string script = @"function reverseCollapse(id) {
//                var dock = $find(id);
//                dock.set_closed(!dock.get_closed());
//                return false;}";
//            RadScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "AdvancedDateTimePicker", script, true);
//            script = "if ($('#" + this.ClientID + @"').css('direction') == 'rtl') {
//                        toggleAllFloat('" + this.ClientID + @"',false);
//                        }";
//            RadScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), this.ClientID, script, true);
//            this.RadComboBoxMonth.Focus();
//        }
//        protected void RadComboBoxCaluture_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
//        {
//            this.setYearAllowedRange();
//            this.getMonthes();
//            CultureInfo ci = CultureInfo.InvariantCulture;
//            if (this.RadComboBoxCaluture.SelectedValue != null)
//            {
//                ci = CultureInfo.CreateSpecificCulture(this.RadComboBoxCaluture.SelectedValue);
//            }
            
//            if (this.SelectedDate != null)
//            {
//                this.setDateControls(this.SelectedDate);
//            }
//            else
//            {
//                try
//                {
//                    this.RadNumericTextBoxYear.Value = ci.Calendar.GetYear(this.Calendar.VisibleDate);
//                }
//                catch (Exception er)
//                {
//                    this.RadNumericTextBoxYear.Value = ci.Calendar.GetYear(ci.Calendar.MinSupportedDateTime);
//                }
//                this.RadComboBoxMonth.SelectedValue = ci.Calendar.GetMonth(this.Calendar.VisibleDate).ToString();
//            }
//        }
//        protected void RadComboBoxMonth_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
//        {
//            CultureInfo ci = CultureInfo.InvariantCulture;
//            if (this.RadComboBoxCaluture.SelectedValue != null)
//            {
//                ci = CultureInfo.CreateSpecificCulture(this.RadComboBoxCaluture.SelectedValue);
//            }
//            if (this.RadNumericTextBoxYear.Value.HasValue && this.RadComboBoxMonth.SelectedValue != "0")
//            {
//                this.Calendar.VisibleDate = new DateTime(Convert.ToInt32(this.RadNumericTextBoxYear.Value), Convert.ToInt32(this.RadComboBoxMonth.SelectedValue), 1, ci.Calendar);
//            }
//            this.RadDock.Closed = false;
//            this.Calendar.Focus();
//            this.RadButtonDate.Text = "...";
//            this.Calendar.SelectedDate = DateTime.MinValue;
//        }
//        protected void RadNumericTextBoxYear_TextChanged(object sender, EventArgs e)
//        {
//            CultureInfo ci = CultureInfo.InvariantCulture;
//            if (this.RadComboBoxCaluture.SelectedValue != null)
//            {
//                ci = CultureInfo.CreateSpecificCulture(this.RadComboBoxCaluture.SelectedValue);
//            }
//            if (this.RadNumericTextBoxYear.Value.HasValue && this.RadComboBoxMonth.SelectedValue != "0")
//            {
//                this.Calendar.VisibleDate = new DateTime(Convert.ToInt32(this.RadNumericTextBoxYear.Value), Convert.ToInt32(this.RadComboBoxMonth.SelectedValue), 1, ci.Calendar);
//            }
//        }
//        protected void RadButtonClear_Click(object sender, EventArgs e)
//        {
//            this.Calendar.SelectedDate = DateTime.MinValue;
//            this.Calendar.VisibleDate = DateTime.Now;
//            //this.RadNumericTextBoxYear.Value = null;
//            this.RadTimePickerTime.SelectedDate = null;
//            this.RadComboBoxMonth.SelectedValue = "0";
//            this.RadButtonDate.Text = "...";
//        }
//        protected void RadButtonToday_Click(object sender, EventArgs e)
//        {
//            this.setDateControls(DateTime.Now.Date);
//        }
//        protected void Calendar_DayRender(object sender, DayRenderEventArgs e)
//        {
//            if (this.MinDate.HasValue)
//            {
//                if (e.Day.Date < this.MinDate)
//                {
//                    e.Day.IsSelectable = false;
//                }
//            }
//            if (this.MaxDate.HasValue)
//            {
//                if (e.Day.Date > this.MaxDate)
//                {
//                    e.Day.IsSelectable = false;
//                }
//            }
//            if (e.Day.Date.Date == DateTime.Now.Date)
//            {
//                //string script = "$('a[href=\""+e.SelectUrl.Replace("'","\\'")+"\"]').parent().focus();";
//                //RadScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), this.ClientID, script, true);
//            }
//        }
//        protected void Calendar_SelectionChanged(object sender, EventArgs e)
//        {
//            this.setDateControls(this.Calendar.SelectedDate);
//            this.RadDock.Closed = true;
//        }


//        private void bindCulters()
//        {
//            this.RadComboBoxCaluture.DataSource = this.Cultures;
//            this.RadComboBoxCaluture.DataTextField = "Key";
//            this.RadComboBoxCaluture.DataValueField = "Value";
//            this.RadComboBoxCaluture.DataBind();
//            if (this.Cultures.Count > 0)
//            {
//                this.RadComboBoxCaluture.SelectedIndex = 0;
//            }
//        }
//        private void setYearAllowedRange()
//        {
//            CultureInfo ci = CultureInfo.InvariantCulture;
//            if (this.RadComboBoxCaluture.SelectedValue != null)
//            {
//                ci = CultureInfo.CreateSpecificCulture(this.RadComboBoxCaluture.SelectedValue);
//            }
//            this.RadNumericTextBoxYear.MinValue = ci.Calendar.GetYear(ci.Calendar.MinSupportedDateTime);
//            this.RadNumericTextBoxYear.MaxValue = ci.Calendar.GetYear(ci.Calendar.MaxSupportedDateTime);
//            this.RadNumericTextBoxYear.Value = ci.Calendar.GetYear(DateTime.Now);
//        }
//        private void getMonthes()
//        {
//            CultureInfo ci = CultureInfo.InvariantCulture;
//            if (this.RadComboBoxCaluture.SelectedValue != null)
//            {
//                ci = CultureInfo.CreateSpecificCulture(this.RadComboBoxCaluture.SelectedValue);
//            }
//            Dictionary<string, int> monthesDictionary = new Dictionary<string, int>();
//            monthesDictionary.Add("---", 0);
//            string[] monthes = ci.DateTimeFormat.MonthNames;
//            int start = 0;
//            int end = monthes.Length - 1;
//            for (int i = start; i < end; i++)
//            {
//                monthesDictionary.Add("("+(i + 1)+")  "+monthes[i], i + 1);
//            }
//            this.RadComboBoxMonth.DataSource = monthesDictionary;
//            this.RadComboBoxMonth.DataTextField = "Key";
//            this.RadComboBoxMonth.DataValueField = "Value";
//            this.RadComboBoxMonth.DataBind();
//            if (monthesDictionary.Count > 0)
//            {
//                this.RadComboBoxMonth.SelectedIndex = 0;
//            }
//        }
//        private DateTime? getDate()
//        {
//            if (this.Calendar.SelectedDate == DateTime.MinValue && this.RadButtonDate.Text == "...")
//            {
//                return null;
//            }
//            else
//            {
//                if (this.RadTimePickerTime.SelectedDate.HasValue)
//                {
//                    return this.Calendar.SelectedDate.Date.AddHours(this.RadTimePickerTime.SelectedDate.Value.Hour).AddMinutes(this.RadTimePickerTime.SelectedDate.Value.Minute).AddSeconds(this.RadTimePickerTime.SelectedDate.Value.Second);
//                }
//                else
//                {
//                    return this.Calendar.SelectedDate.Date;
//                }
//            }
//        }
//        private void setDateControls(DateTime? value)
//        {
//            if (value.HasValue)
//            {
//                CultureInfo ci = CultureInfo.InvariantCulture;
//                if (this.RadComboBoxCaluture.SelectedValue != null)
//                {
//                    ci = CultureInfo.CreateSpecificCulture(this.RadComboBoxCaluture.SelectedValue);
//                }
//                if (value > ci.Calendar.MaxSupportedDateTime)
//                {
//                    value = ci.Calendar.MaxSupportedDateTime.Date;
//                }
//                if (value < ci.Calendar.MinSupportedDateTime)
//                {
//                    value = ci.Calendar.MinSupportedDateTime.Date;
//                }
//                this.Calendar.SelectedDate = value.Value.Date;
//                this.Calendar.VisibleDate = value.Value.Date;
//                this.RadNumericTextBoxYear.Value = ci.Calendar.GetYear(value.Value.Date);
//                this.RadComboBoxMonth.SelectedValue = ci.Calendar.GetMonth(value.Value.Date).ToString();
//                this.RadButtonDate.Text = ci.Calendar.GetDayOfMonth(value.Value.Date).ToString();
//            }
//            else
//            {
//                this.Calendar.SelectedDate = DateTime.MinValue.Date;
//                this.Calendar.VisibleDate = DateTime.MinValue.Date;
//                this.RadNumericTextBoxYear.Value = null;
//                if (this.RadComboBoxMonth.DataSource == null)
//                {
//                    this.getMonthes();
//                }
//                this.RadComboBoxMonth.SelectedValue = "0";
//                this.RadButtonDate.Text = "---";
//            }
//        }
//        private void setTimeControl(DateTime? value)
//        {
//            this.RadTimePickerTime.SelectedDate = value;
//        }


//    }
//}


//============================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.UI.HtmlControls;

namespace TelerikHelper
{
    [DefaultProperty("SelectedDate")]
    [ToolboxData("<{0}:AdvancedDateTimePicker runat=server></{0}:AdvancedDateTimePicker>")]
    public class AdvancedDateTimePicker2 : CompositeControl
    {
        public Dictionary<string, string> Cultures
        {
            get
            {
                if (this.ViewState["Cultures"] == null)
                {
                    this.ViewState["Cultures"] = new Dictionary<string, string>() { { "هـ", "ar-sa" }, { "م", "en" } };
                }
                return (Dictionary<string, string>)this.ViewState["Cultures"];
            }
            set
            {
                this.ViewState["Cultures"] = value;
            }
        }
        public CultureInfo CurrentCulture
        {
            get
            {
                CultureInfo ci = CultureInfo.InvariantCulture;
                if (this.RadComboBoxCaluture.SelectedValue != null)
                {
                    ci = new CultureInfo(CultureInfo.CreateSpecificCulture(this.RadComboBoxCaluture.SelectedValue).LCID, false);
                }
                ci.DateTimeFormat.Calendar = new GregorianCalendar();
                return ci;
            }
        }
        public DateTime MinDate
        {
            get
            {
                this.EnsureChildControls();
                return this.Calendar.RangeMinDate;
            }
            set
            {
                this.EnsureChildControls();
                this.RadButtonToday.Enabled = DateTime.Now >= value;
                this.Calendar.RangeMinDate = value;
            }
        }
        public DateTime MaxDate
        {
            get
            {
                this.EnsureChildControls();
                return this.Calendar.RangeMaxDate;
            }
            set
            {
                this.EnsureChildControls();
                this.RadButtonToday.Enabled = DateTime.Now <= value;
                this.Calendar.RangeMaxDate = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(null)]
        [Localizable(true)]
        public DateTime? SelectedDate
        {
            get
            {
                return (DateTime?)this.ViewState["SelectedDate"];
            }
            set
            {
                this.ViewState["SelectedDate"] = value;
                if (this.ChildControlsCreated)
                {
                    this.setDateControls(value);
                    this.setTimeControl(value);
                }
            }
        }

        public object DBSelectedDate
        {
            get
            {
                return this.SelectedDate.HasValue ? (object)this.SelectedDate.Value : DBNull.Value;            
            }
            set
            {
                this.SelectedDate = (value == DBNull.Value ? (DateTime?)null : (DateTime?)value);
            }

        }


        public RadNumericTextBox RadNumericTextBoxYear=null;
        public RadComboBox RadComboBoxMonth = null;
        public RadButton RadButtonDate = null;
        public RadDock RadDock = null;
        public RadTimePicker RadTimePickerTime = null;
        public RadComboBox RadComboBoxCaluture = null;
        public RadButton RadButtonClear = null;
        public RadCalendar Calendar = null;
        public RadButton RadButtonToday = null;

        public AdvancedDateTimePicker2(): base()
        {
            this.Load+=AdvancedDateTimePicker_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            RadScriptManager.RegisterClientScriptResource(this,this.GetType(), "TelerikHelper.Scripts.HelperMethods.js");
        }
        protected override void CreateChildControls()
        {
            if (!ChildControlsCreated)
            {
                this.Controls.Clear();
                               
                //generic Div
                HtmlGenericControl DivContainer = new HtmlGenericControl("div");
                DivContainer.ID = "DivContainer";
                this.Controls.Add(DivContainer);

                //ul
                HtmlGenericControl UL = new HtmlGenericControl("ul");
                UL.Style["margin"] = "0px";
                UL.Style["padding"] = "0px";
                UL.Style["list-style-type"] = "none";
                DivContainer.Controls.Add(UL);

                //li
                HtmlGenericControl LI = new HtmlGenericControl("li");
                LI.Style["margin"] = "0px";
                LI.Style["padding"] = "1px";
                LI.Style["float"] = "left";
                UL.Controls.Add(LI);

                //RadButtonDate
                this.RadButtonDate = new RadButton();
                this.RadButtonDate.ID = "RadButtonDate";
                this.RadButtonDate.AutoPostBack = false;
                this.RadButtonDate.Text = "...";
                this.RadButtonDate.Width = Unit.Pixel(35);
                this.RadButtonDate.Style["margin"] = "2px";
                this.RadButtonDate.CausesValidation = false;
                LI.Controls.Add(this.RadButtonDate);

                

                //li
                LI = new HtmlGenericControl("li");
                LI.Style["margin"] = "0px";
                LI.Style["padding"] = "1px";
                LI.Style["float"] = "left";
                UL.Controls.Add(LI);

                //RadComboBoxMonth
                this.RadComboBoxMonth = new RadComboBox();
                this.RadComboBoxMonth.ID = "RadComboBoxMonth";
                this.RadComboBoxMonth.SelectedIndexChanged += RadComboBoxMonth_SelectedIndexChanged;
                this.RadComboBoxMonth.AutoPostBack = true;
                this.RadComboBoxMonth.Width = Unit.Pixel(110);
                this.RadComboBoxMonth.Style["margin"] = "2px";
                this.RadComboBoxMonth.AllowCustomText = false;
                this.RadComboBoxMonth.Filter = RadComboBoxFilter.Contains;
                this.RadComboBoxMonth.EnableTextSelection = true;
                this.RadComboBoxMonth.MarkFirstMatch = true;
                this.RadComboBoxMonth.ShowDropDownOnTextboxClick = true;
                this.RadComboBoxMonth.CausesValidation = false;
                LI.Controls.Add(this.RadComboBoxMonth);

                //li
                LI = new HtmlGenericControl("li");
                LI.Style["margin"] = "0px";
                LI.Style["padding"] = "1px";
                LI.Style["float"] = "left";
                UL.Controls.Add(LI);

                //RadNumericTextBoxYear
                this.RadNumericTextBoxYear = new RadNumericTextBox();
                this.RadNumericTextBoxYear.ID = "RadNumericTextBoxYear";
                this.RadNumericTextBoxYear.TextChanged += RadNumericTextBoxYear_TextChanged;
                this.RadNumericTextBoxYear.Width = Unit.Pixel(60);
                this.RadNumericTextBoxYear.NumberFormat.DecimalDigits = 0;
                this.RadNumericTextBoxYear.NumberFormat.GroupSeparator = "";
                this.RadNumericTextBoxYear.Style["margin"] = "2px";
                this.RadNumericTextBoxYear.CausesValidation = false;
                this.RadNumericTextBoxYear.ShowSpinButtons = true;
                LI.Controls.Add(this.RadNumericTextBoxYear);

                //li
                LI = new HtmlGenericControl("li");
                LI.Style["clear"] = "both";
                UL.Controls.Add(LI);


                //RadDockZone
                RadDockZone RadDockZone = new RadDockZone();
                RadDockZone.MinHeight = Unit.Pixel(0);
                RadDockZone.BorderColor = System.Drawing.Color.Transparent;
                RadDockZone.FitDocks = true;
                RadDockZone.Width = Unit.Pixel(265);
                RadDockZone.Style["position"] = "absolute";
                RadDockZone.Style["z-index"] = "1000";
                DivContainer.Controls.Add(RadDockZone);

                //RadDock
                this.RadDock = new RadDock();
                this.RadDock.ID = "RadDock";
                this.RadDock.UniqueName = "RadDock_" + this.ClientID;
                this.RadDock.Closed = true;
                this.RadDock.DefaultCommands = Telerik.Web.UI.Dock.DefaultCommands.None;
                this.RadDock.DockHandle = DockHandle.None;
                this.RadDock.EnableRoundedCorners = false;
                this.RadDock.Resizable = true;
                RadDockZone.Controls.Add(this.RadDock);
                this.RadButtonDate.OnClientClicked = "function(){ reverseCollapse('" + this.RadDock.ClientID + "');}";


                //ul
                UL = new HtmlGenericControl("ul");
                UL.Style["margin"] = "0px";
                UL.Style["padding"] = "0px";
                UL.Style["list-style-type"] = "none";
                RadDock.ContentContainer.Controls.Add(UL);


                //li
                LI = new HtmlGenericControl("li");
                LI.Style["margin"] = "0px";
                LI.Style["padding"] = "1px";
                LI.Style["float"] = "left";
                UL.Controls.Add(LI);


                //RadTimePickerTime
                this.RadTimePickerTime = new RadTimePicker();
                this.RadTimePickerTime.ID = "RadTimePickerTime";
                this.RadTimePickerTime.Width = Unit.Pixel(93);
                this.RadTimePickerTime.Style["margin"] = "2px";
                LI.Controls.Add(this.RadTimePickerTime);


                //li
                LI = new HtmlGenericControl("li");
                LI.Style["margin"] = "0px";
                LI.Style["padding"] = "1px";
                LI.Style["float"] = "left";
                UL.Controls.Add(LI);


                //RadComboBoxCaluture
                this.RadComboBoxCaluture = new RadComboBox();
                this.RadComboBoxCaluture.ID = "RadComboBoxCaluture";
                this.RadComboBoxCaluture.SelectedIndexChanged += RadComboBoxCaluture_SelectedIndexChanged;
                this.RadComboBoxCaluture.AutoPostBack = true;
                this.RadComboBoxCaluture.Width = Unit.Pixel(75);
                this.RadComboBoxCaluture.Style["margin"] = "2px";
                this.RadComboBoxCaluture.AllowCustomText = false;
                this.RadComboBoxCaluture.Filter = RadComboBoxFilter.Contains;
                this.RadComboBoxCaluture.MarkFirstMatch = true;
                this.RadComboBoxCaluture.EnableTextSelection = true;
                this.RadComboBoxCaluture.ShowDropDownOnTextboxClick = true;
                this.RadComboBoxCaluture.CausesValidation = false;
                this.RadComboBoxCaluture.ZIndex = 1001;
                LI.Controls.Add(this.RadComboBoxCaluture);

                //li
                LI = new HtmlGenericControl("li");
                LI.Style["margin"] = "0px";
                LI.Style["padding"] = "1px";
                LI.Style["float"] = "left";
                UL.Controls.Add(LI);



                //RadButtonClear
                this.RadButtonClear = new RadButton();
                this.RadButtonClear.ID = "RadButtonClear";
                this.RadButtonClear.Click += RadButtonClear_Click;
                this.RadButtonClear.Icon.SecondaryIconCssClass = "rbRemove";
                this.RadButtonClear.Text = "";
                this.RadButtonClear.Style["margin"] = "2px";
                this.RadButtonClear.CausesValidation = false;
                LI.Controls.Add(this.RadButtonClear);

                //li
                LI = new HtmlGenericControl("li");
                LI.Style["clear"] = "both";
                UL.Controls.Add(LI);

                //Calendar
                this.Calendar = new RadCalendar();
                this.Calendar.ID = "Calendar";
                this.Calendar.SelectionChanged += Calendar_SelectionChanged;
                this.Calendar.DayRender +=Calendar_DayRender;
                this.Calendar.EnableMultiSelect = false;
                this.Calendar.ShowRowHeaders = false;
                this.Calendar.ShowNavigationButtons = false;
                this.Calendar.EnableMonthYearFastNavigation = false;
                this.Calendar.EnableNavigation = false;
                //this.Calendar.EnableKeyboardNavigation = true;
                this.Calendar.HideNavigationControls = true;
                this.Calendar.Style["margin"] = "2px";
                this.Calendar.TitleFormat = "MM";
                this.Calendar.DayNameFormat = DayNameFormat.Short;
                RadDock.ContentContainer.Controls.Add(this.Calendar);

                //generic Div
                HtmlGenericControl div = new HtmlGenericControl("div");
                div.Style["width"] = "80px";
                div.Style["margin"] = "0 auto";
                RadDock.ContentContainer.Controls.Add(div);

                //RadButtonToday
                this.RadButtonToday = new RadButton();
                this.RadButtonToday.ID = "RadButtonToday";
                this.RadButtonToday.Click += RadButtonToday_Click;
                this.RadButtonToday.CausesValidation = false;
                div.Controls.Add(this.RadButtonToday);


                this.setDateControls(this.SelectedDate);
                this.setTimeControl(this.SelectedDate);


                this.ChildControlsCreated = true;

            }
        }

        private void Calendar_DayRender(object sender, Telerik.Web.UI.Calendar.DayRenderEventArgs e)
        {
            string day="";
            try
            {
                day=this.CurrentCulture.Calendar.GetDayOfMonth(e.Day.Date).ToString();
            }
            catch(Exception er)
            {

            }
            e.Cell.Text = e.Cell.Text.Replace(e.Day.Date.Day.ToString(), day);
        }
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected void AdvancedDateTimePicker_Load(object sender, EventArgs e)
        {
            this.EnsureChildControls();
            if (!this.Page.IsPostBack)
            {
                this.bindCulters();
                this.setAllowedRange();
                this.getMonthes();
                this.Calendar.FocusedDate = DateTime.Now.Date;
                this.RadButtonToday.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
            }
            
            string script = @"function reverseCollapse(id) {
                var dock = $find(id);
                dock.set_closed(!dock.get_closed());
                return false;}";
            RadScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "AdvancedDateTimePicker", script, true);
            script = "if ($('#" + this.ClientID + @"').css('direction') == 'rtl') {
                        toggleAllFloat('" + this.ClientID + @"',false);
                        }";
            RadScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), this.ClientID, script, true);
            this.RadComboBoxMonth.Focus();
        }
        protected void RadComboBoxCaluture_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.Calendar.CultureInfo = this.CurrentCulture;
            DateTime? currentDatetime = this.SelectedDate;
            this.setAllowedRange();
            this.getMonthes();

            if (currentDatetime != null)
            {
                this.setDateControls(currentDatetime);
            }
            else
            {

                this.RadNumericTextBoxYear.Value = this.CurrentCulture.Calendar.GetYear(this.Calendar.FocusedDate);
                this.RadComboBoxMonth.SelectedValue = this.CurrentCulture.Calendar.GetMonth(this.Calendar.FocusedDate).ToString();
            }
        }
        protected void RadComboBoxMonth_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.Calendar.CultureInfo = this.CurrentCulture;
            this.Calendar.SelectedDate = this.CurrentCulture.Calendar.MinSupportedDateTime;
            if (this.RadNumericTextBoxYear.Value.HasValue && this.RadComboBoxMonth.SelectedValue != "0")
            {
                this.Calendar.FocusedDate = new DateTime(Convert.ToInt32(this.RadNumericTextBoxYear.Value), Convert.ToInt32(this.RadComboBoxMonth.SelectedValue), 1, this.CurrentCulture.Calendar);
            }
            this.Calendar.Focus();
            this.RadButtonDate.Text = "...";
            this.RadDock.Closed = false;
        }
        protected void RadNumericTextBoxYear_TextChanged(object sender, EventArgs e)
        {
            this.setAllowedRange();
            this.Calendar.CultureInfo = this.CurrentCulture;
            this.Calendar.SelectedDate = this.CurrentCulture.Calendar.MinSupportedDateTime;
            if (this.RadNumericTextBoxYear.Value.HasValue && this.RadComboBoxMonth.SelectedValue != "0")
            {
                this.Calendar.FocusedDate = new DateTime(Convert.ToInt32(this.RadNumericTextBoxYear.Value), Convert.ToInt32(this.RadComboBoxMonth.SelectedValue), 1, this.CurrentCulture.Calendar);
            }
        }
        protected void RadButtonClear_Click(object sender, EventArgs e)
        {
            this.Calendar.SelectedDate = this.CurrentCulture.Calendar.MinSupportedDateTime;
            this.Calendar.FocusedDate = DateTime.Now;
            this.RadTimePickerTime.SelectedDate = null;
            this.RadComboBoxMonth.SelectedValue = "0";
            this.RadButtonDate.Text = "...";
        }
        protected void RadButtonToday_Click(object sender, EventArgs e)
        {
            this.Calendar.CultureInfo = this.CurrentCulture;
            this.setDateControls(DateTime.Now.Date);
        }
        protected void Calendar_SelectionChanged(object sender, EventArgs e)
        {
            this.setDateControls(this.Calendar.SelectedDate);
            this.RadDock.Closed = true;
        }


        private void bindCulters()
        {
            this.RadComboBoxCaluture.DataSource = this.Cultures;
            this.RadComboBoxCaluture.DataTextField = "Key";
            this.RadComboBoxCaluture.DataValueField = "Value";
            this.RadComboBoxCaluture.DataBind();
            if (this.Cultures.Count > 0)
            {
                this.RadComboBoxCaluture.SelectedIndex = 0;
            }
            this.Calendar.CultureInfo = this.CurrentCulture;
        }
        private void setAllowedRange()
        {
            this.RadNumericTextBoxYear.MinValue = this.CurrentCulture.Calendar.GetYear(this.CurrentCulture.Calendar.MinSupportedDateTime);
            this.Calendar.RangeMinDate = this.CurrentCulture.Calendar.MinSupportedDateTime;
            this.RadNumericTextBoxYear.MaxValue = this.CurrentCulture.Calendar.GetYear(this.CurrentCulture.Calendar.MaxSupportedDateTime);
            this.Calendar.RangeMaxDate = this.CurrentCulture.Calendar.MaxSupportedDateTime;
            this.RadNumericTextBoxYear.Value = this.CurrentCulture.Calendar.GetYear(DateTime.Now);
        }
        private void getMonthes()
        {
            Dictionary<string, int> monthesDictionary = new Dictionary<string, int>();
            monthesDictionary.Add("---", 0);
            string[] monthes = this.CurrentCulture.DateTimeFormat.MonthNames;
            int start = 0;
            int end = monthes.Length - 1;
            for (int i = start; i < end; i++)
            {
                monthesDictionary.Add("("+(i + 1)+")  "+monthes[i], i + 1);
            }
            this.RadComboBoxMonth.DataSource = monthesDictionary;
            this.RadComboBoxMonth.DataTextField = "Key";
            this.RadComboBoxMonth.DataValueField = "Value";
            this.RadComboBoxMonth.DataBind();
            if (monthesDictionary.Count > 0)
            {
                this.RadComboBoxMonth.SelectedIndex = 0;
            }
        }
        private DateTime? getDate()
        {
            if (this.RadComboBoxMonth.SelectedValue != null && this.RadButtonDate.Text!="...")
            {
                DateTime date = new DateTime(Convert.ToInt32(this.RadNumericTextBoxYear.Text), Convert.ToInt32(this.RadComboBoxMonth.SelectedValue), Convert.ToInt32(this.RadButtonDate.Text), this.CurrentCulture.Calendar);
                if (this.RadTimePickerTime.SelectedDate.HasValue)
                {
                    date = date.AddHours(this.RadTimePickerTime.SelectedDate.Value.Hour).AddMinutes(this.RadTimePickerTime.SelectedDate.Value.Minute).AddSeconds(this.RadTimePickerTime.SelectedDate.Value.Second);
                }
                return date;
            }
            return null;
        }
        private void setDateControls(DateTime? value)
        {
            if (value.HasValue)
            {
                if (value > this.MaxDate)
                {
                    value = this.CurrentCulture.Calendar.MaxSupportedDateTime.Date;
                    this.SelectedDate = value;
                }
                if (value < this.MinDate)
                {
                    value = this.CurrentCulture.Calendar.MinSupportedDateTime.Date;
                    this.SelectedDate = value;
                }
                this.Calendar.SelectedDate = value.Value.Date;
                this.Calendar.FocusedDate = value.Value.Date;
                this.RadNumericTextBoxYear.Value = this.CurrentCulture.Calendar.GetYear(value.Value.Date);
                this.RadComboBoxMonth.SelectedValue = this.CurrentCulture.Calendar.GetMonth(value.Value.Date).ToString();
                this.RadButtonDate.Text = this.CurrentCulture.Calendar.GetDayOfMonth(value.Value.Date).ToString();
            }
            else
            {
                this.Calendar.SelectedDate = this.CurrentCulture.Calendar.MinSupportedDateTime;
                this.Calendar.FocusedDate = DateTime.Now;
                this.RadNumericTextBoxYear.Value = null;
                if (this.RadComboBoxMonth.DataSource == null)
                {
                    this.getMonthes();
                }
                this.RadComboBoxMonth.SelectedValue = "0";
                this.RadButtonDate.Text = "---";
            }
        }
        private void setTimeControl(DateTime? value)
        {
            this.RadTimePickerTime.SelectedDate = value;
        }


    }
}

